package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberCheckStateEntity;
import com.sgb.member.entity.MemberLogisticsEntity;
import com.sgb.member.ui.fragment.BusinessLogisticsFragment;
import com.sgb.member.ui.fragment.BusinessLogisticsFragment2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BusinessLogisticsMode2 extends BaseViewModel {

    private final BusinessLogisticsFragment2 fragment;
    private final MemberRepertory mRepertory;
    private final int maxPageSize = 10;
    private int current;

    private CommonPopEntity mAll;

    public BusinessLogisticsMode2(BusinessLogisticsFragment2 fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    // 物流货物
    public void getLogisticsType() {
        mRepertory.getApi().goodsType().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, new CommonPopEntity("", "全部"));
                    fragment.setOneTabType(data.getData());
                } else {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                showOrHideDialog(false);
                fragment.showEmpty();
            }

            @Override
            public void onFinally() {

            }
        });
    }

    public void getFilterSourceList( boolean isLoadMore,
                                    String goodsModeNo,
                                    String twoLeve,
                                    String mNo1,
                                    String loadingTime,
                                    GoodsSourceLocationEntity mStartEntity,
                                    GoodsSourceLocationEntity mEndEntity, String level) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        map.put("limit", maxPageSize);
        map.put("goodsModeNo", goodsModeNo); // 货物类型编号
        map.put("secondGoodsTypeNo", twoLeve);

        if (mStartEntity != null) {
            map.put("provinceNo", StringUtil.getNotEmptyStr(mStartEntity.getProvinceNo())); // 发货地-省
            map.put("cityNo", StringUtil.getNotEmptyStr(mStartEntity.getCityNo())); // 发货地-市
            map.put("districtNo", StringUtil.getNotEmptyStr(mStartEntity.getAreaNo())); // 发货地-区
        }
        if (mEndEntity != null) {
            map.put("destProvinceNo", StringUtil.getNotEmptyStr(mEndEntity.getProvinceNo())); // 目的地-省
            map.put("destCityNo", StringUtil.getNotEmptyStr(mEndEntity.getCityNo())); // 目的地-市
            map.put("destDistrictNo", StringUtil.getNotEmptyStr(mEndEntity.getAreaNo())); // 目的地-区
        }
        map.put("loadingTime", loadingTime);
        extractedWeight(mNo1, map);
        map.put("level", level);
        map.put("pattern", "0"); // 0 普通货源,1 竞价货源
        map.put("areacode", mRepertory.getAreaCode());
        map.put("isApp", 1);
        showOrHideDialog(true);

        mRepertory.getApi().getFilterGoodsSourceList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberLogisticsEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberLogisticsEntity>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberLogisticsEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }
        });
    }

    public void getTimeData() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("0", "全部"));
//
//        Date now = new Date();
//        Calendar nowCal = Calendar.getInstance();
//        nowCal.setTime(now);
//        nowCal.set(Calendar.HOUR_OF_DAY, 23);
//        nowCal.set(Calendar.MINUTE, 59);
//        nowCal.set(Calendar.SECOND, 59);
//        nowCal.set(Calendar.MILLISECOND, 0);
        list.add(new CommonPopEntity( "1", "今天"));


//        Calendar tomorrow = Calendar.getInstance();
//        tomorrow.setTime(now);
//        tomorrow.add(Calendar.DATE, 1);
//        tomorrow.set(Calendar.HOUR_OF_DAY, 23);
//        tomorrow.set(Calendar.MINUTE, 59);
//        tomorrow.set(Calendar.SECOND, 59);
//        tomorrow.set(Calendar.MILLISECOND, 0);
        list.add(new CommonPopEntity( "2", "明天"));

//        Calendar later = Calendar.getInstance();
//        later.setTime(now);
//        later.add(Calendar.DATE, 2);
//        later.set(Calendar.HOUR_OF_DAY, 0);
//        later.set(Calendar.MINUTE, 0);
//        later.set(Calendar.SECOND, 0);
//        later.set(Calendar.MILLISECOND, 0);
        list.add(new CommonPopEntity("3", "明天以后"));
        fragment.setTimeData(list);
    }

    //会员级别
    public void getMemberLevelData() {
        mRepertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setMemberLevelData(data.getData());
                } else {
                    fragment.setMemberLevelData(new ArrayList<>());
                }
            }
        });
    }

    public void getWeight() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("0", "0-30吨"));
        list.add(new CommonPopEntity("1", "31-100吨"));
        list.add(new CommonPopEntity("2", "101-500吨"));
        list.add(new CommonPopEntity("3", "501-1000吨"));
        list.add(new CommonPopEntity("4", "1000-2000吨"));
        list.add(new CommonPopEntity("5", "2000吨以上"));
        fragment.setWeightData(list);
    }

    private void extractedWeight(String weightNo, Map<String, Object> map) {
        int volume = TextUtils.isEmpty(weightNo) ? -1 : Integer.parseInt(weightNo);
        switch (volume) {
            case 0:
                map.put("minWeight", "0");
                map.put("maxWeight", "30");
                break;
            case 1:
                map.put("minWeight", "31");
                map.put("maxWeight", "100");
                break;
            case 2:
                map.put("minWeight", "101");
                map.put("maxWeight", "500");
                break;
            case 3:
                map.put("minWeight", "501");
                map.put("maxWeight", "1000");
                break;
            case 4:
                map.put("minWeight", "1001");
                map.put("maxWeight", "2000");
                break;
            case 5:
                map.put("minWeight", "2000");
                map.put("maxWeight", "");
                break;
            default:
                map.put("minWeight", "");
                map.put("maxWeight", "");
                break;
        }
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel() {
        showOrHideDialog(true);
        mRepertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressData(data.getData());
                }
            }
        });
    }

    public void goodsSourceStatusCheck(String no, int position) {
        HashMap map = new HashMap();
        map.put("no", no);
        mRepertory.getApi().goodsSourceStatusCheck(map).enqueue(new Callback<BaseEntity<MemberCheckStateEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MemberCheckStateEntity>> call, Response<BaseEntity<MemberCheckStateEntity>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    fragment.checkGoodsState(position, response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberCheckStateEntity>> call, Throwable t) {

            }
        });
    }


}
